
/**
 * 去掉首字符.
 */
export const removeFirstDot = (str: string) => {
    if (str) {
        return str.replace(WLConst.reg.firstDot, '');
    }
    return '';
}


/**
 * 去掉最后一节 .abc 带 .
 */
export const removeLastDotVar = (str: string) => {
    if (str) {
        return str.replace(WLConst.reg.lastDotVar, '');
    }
    return '';
}


/**
 * 根据 json 生成对应的 conf
 * @param json 数据 json
 * @param parentKey 父级 key
 * @param level 当前级别
 */
export const createJsonConf = (json: any, parentKey: string, level: number): any => {
    parentKey = parentKey.replace(WLConst.reg.firstDot, '');
    if (wlIsObject(json)) {
        let outerConf: JsonConf = {} as any;
        outerConf.key = parentKey;
        outerConf.list = [];
        outerConf.type = 'object';
        outerConf.level = level;
        outerConf.flag = true;
        outerConf.selected = false;

        for (var key in json) {
            let conf: JsonConf = createJsonConf(json[key], parentKey + '.' + key, level + 1);
            conf.prop = key;
            outerConf.list.push(conf);
        }
        return outerConf;
    } else if (wlIsArray(json)) {
        let outerConf: JsonConf = {} as any;
        outerConf.type = 'array';
        outerConf.flag = true;
        outerConf.list = [];
        outerConf.selected = false;
        outerConf.key = parentKey;
        json.forEach((item: any, ind: number) => {
            let tmpConf: JsonConf = createJsonConf(item, parentKey + '[' + ind + ']', level + 1);
            tmpConf.prop = ind + '';
            outerConf.list.push(tmpConf);
        });

        return outerConf;
    } else if (wlIsString(json)) {
        let outerConf: JsonConf = {} as any;
        outerConf.type = 'string';
        outerConf.key = parentKey + '.string';
        outerConf.prop = parentKey;
        outerConf.level = level;
        outerConf.excelFormula = '';
        outerConf.selected = false;
        return outerConf;
    } else if (wlIsNumber(json)) {
        let outerConf: JsonConf = {} as any;
        outerConf.type = 'number';
        outerConf.key = parentKey + '.number';
        outerConf.prop = parentKey;
        outerConf.level = level;
        outerConf.excelFormula = '';
        outerConf.selected = false;
        return outerConf;
    } else if (wlIsBoolean(json)) {
        let outerConf: JsonConf = {} as any;
        outerConf.type = 'boolean';
        outerConf.key = parentKey + '.boolean';
        outerConf.prop = parentKey;
        outerConf.level = level;
        outerConf.excelFormula = '';
        outerConf.selected = false;
        return outerConf;
    } else {
        let outerConf: JsonConf = {} as any;
        outerConf.type = 'none';
        outerConf.key = parentKey + '.none';
        outerConf.prop = parentKey;
        outerConf.level = level;
        outerConf.excelFormula = '';
        outerConf.selected = false;
        return outerConf;
    }
};


/**
 * 找出数组下标的索引
 * 
 * @param key 属性名 abd.list[2]
 */
export const getArrayIndex = (key: string): number => {
    var str: string = key.replace(WLConst.reg.arrayInd, '$1');
    var val: number = parseInt(str);
    if (val >= 0) {
        return val;
    } else {
        return -1;
    }
}


/**
 * 获取 json 对象数据
 * @param json 数据对象
 * @param key 属性名 abd.list[2].name
 */
export const getJsonVal = (json: any, key: string): any => {
    if (!(key && key != '')) {
        return json;
    }
    var keyPrefix: string;
    var type: string;
    if (WLConst.reg.lastType.test(key)) {
        keyPrefix = removeLastDotVar(key);
        type = key.replace(keyPrefix, '');
        type = removeFirstDot(type);
    } else {
        type = 'any';
        keyPrefix = key;
    }
    var keyList: string[] = keyPrefix.match(WLConst.reg.jsonKey) as any;

    //包含数组链式
    if (keyList) {
        let tmpStr: string = keyList.join('');
        let arrayTailVar: string = keyPrefix.replace(tmpStr, '');
        arrayTailVar = removeFirstDot(arrayTailVar);

        var tmp: any = json;
        keyList.forEach((k: string, ind: number) => {
            tmp = getJsonArrayVal(tmp, k);
        });

        if (wlIsObject(tmp)) {
            return getJsonVal(tmp, arrayTailVar);
        } else {
            return tmp;
        }
    } else {
        keyList = keyPrefix.split('.');
        let len: number = keyList.length;
        let tmp: any = json;
        for (var i = 0; i < len - 1; i++) {
            tmp = tmp[keyList[i]];
        }

        return tmp[keyList[len - 1]];
    }
};


/**
 * 获取 json 数组里面的某个元素
 * 
 * @param json 数据对象
 * @param key 属性名: abcd.list[0]
 */
export const getJsonArrayVal = (json: any, key: string): any => {
    var tmpKey: string = key.replace(/\[[0-9]+\]/, '');
    var ind: number = getArrayIndex(key);

    var keyList: string[] = tmpKey.split('.');
    var tmp: any = json;
    for (var i = 0; i < keyList.length; i++) {
        tmp = tmp[keyList[i]];
    }

    try {
        if (wlIsArray(tmp)) {
            return tmp[ind];
        }
    } catch (e) {

        console.warn('array val error', e);
    }
    return null;
}


export const getJsonKeys = (json: any): string[] => {
    var strList: string[] = [];
    for (var key in json) {
        if (wlIsObject(json[key])) {
            let keys: string[] = getJsonKeys(json[key]);
            keys = keys.map((inner: string) => {
                return key + '.' + inner;
            });
            strList = strList.concat(keys);

        } else if (wlIsArray(json[key])) {
            if (json[key].length > 0) {
                let tmp: any = json[key][0];
                if (wlIsObject(tmp)) {
                    let keys: string[] = getJsonKeys(tmp);
                    keys = keys.map((inner: string) => {
                        return key + '[].' + inner;
                    });

                    strList = strList.concat(keys);
                }else{
                    strList.push(key + '[].' + wlGetType(tmp));
                }
            }else{
                //todo alert error
            }
        } else {
            strList.push(key + '.' + wlGetType(json[key]));
        }

    }

    return strList;
}